package com.yunxi.service.dubboService.impl.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.account.pojo.AccountGroupVO;
import com.yunxi.account.service.AccountGroupDubboService;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.model.account.AccountGroup;
import com.yunxi.model.account.AccountGroupProp;
import com.yunxi.service.localService.account.AccountGroupPropService;
import com.yunxi.service.localService.account.AccountGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 帐户组 服务实现类
 * </p>
 *
 * @author
 * @since 2019-05-18
 */
//@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
//        application = "${dubbo.application.id}",
//        protocol = "${dubbo.protocol.id}",
//        registry = "${dubbo.registry.id}"
//)
public class AccountGroupDubboServiceImpl implements AccountGroupDubboService {

   @Autowired
   @Qualifier(value = "accountGroupServiceImpl")
   AccountGroupService accountGroupService;

   @Autowired
   @Qualifier(value = "accountGroupPropServiceImpl")
   AccountGroupPropService accountGroupPropService;


   @Override
   public AccountGroupVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      AccountGroup accountGroup = accountGroupService.getById(id);
      AccountGroupVO accountGroupVO = new AccountGroupVO();
      BeanUtils.copyProperties(accountGroup, accountGroupVO);
      return accountGroupVO;
   }

   @Override
   public void saveOrUpdate(AccountGroupVO accountGroupVO, LoginInfo loginInfo) throws InvocationTargetException, IllegalAccessException {
      if (StringUtils.isEmpty(accountGroupVO.getAccountGroupId())) {
         accountGroupVO.setCreateBy(loginInfo.getRealName());
      }
      accountGroupVO.setUpdateBy(loginInfo.getRealName());
      AccountGroup accountGroup = new AccountGroup();
      BeanUtils.copyProperties(accountGroupVO, accountGroup);
      accountGroupService.saveOrUpdate(accountGroup);
   }

   @Override
   public void removeById(String id) {
      accountGroupService.removeById(id);
   }

   @Override
   public PageInfo<AccountGroupVO> findPageByVO(AccountGroupVO accountGroupVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {

      PageInfo<AccountGroupVO> pageInfo = accountGroupService.pageByVo(accountGroupVO, pageNum, pageSize);

      return pageInfo;

   }

   @Override
   public AccountGroupVO load(String accountGroupCode) {

      return accountGroupService.load(accountGroupCode);
   }

   @Override
   public List listByVo(AccountGroupVO accountGroupVO) {
      return accountGroupService.listByVo(accountGroupVO);
   }

   @Override
   public void removeByListVo(List<AccountGroupVO> accountGroupVOList, LoginInfo loginInfo) throws IllegalAccessException, InvocationTargetException, InstantiationException {
      for (AccountGroupVO accountGroupVO : accountGroupVOList) {
         accountGroupVO.setUpdateBy(loginInfo.getRealName());

         QueryWrapper<AccountGroupProp> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("ACCOUNT_GROUP_CODE",accountGroupVO.getAccountGroupCode());
         accountGroupPropService.remove(queryWrapper);
      }
      accountGroupService.removeByListVo(accountGroupVOList);
   }

}
